package com.laoyao.util;


import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Random;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

import com.jfinal.upload.UploadFile;

public class FileUtil {
    

	/**
	 * 传入UploadFile和你想要保存的
	 *地址就能移动文件到你想要的地址了 
	 * @param upFile UploadFile类型
	 * @param path   你要保存的路径
	 * @return       重新命名的文件名
	 */
	
	public static String save(UploadFile upFile,String path){
		String name = null;
		File file = upFile.getFile();
		String fileName = upFile.getFileName();
		
		if (file != null) {
			String newFileName = String.valueOf(DateUtil.getyyyyMMddHHmmssSSS()
					+ new Random().nextInt(1000));
			// 获取文件后缀名
			String suffix = fileName.substring(fileName.lastIndexOf("."));
			// 组成新文件名
			newFileName += suffix;
			name = path + newFileName;
			// 判断文件夹是否存在，不存在就创建
			File myfile = new File(name);
			if (!myfile.getParentFile().exists()) {
				myfile.getParentFile().mkdirs();
			}
			System.out.println(myfile.getParentFile());
			// 直接移动文件到指定的文件夹
			// file.renameTo(myfile);
			try {
				Files.move(file.toPath(), myfile.toPath(),
						StandardCopyOption.ATOMIC_MOVE);
				name = newFileName;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
			
			
		
		
		return name;
	}
	
	/**
	 * 还未测试，先写着，以后备用 
	 * 
	 * @param file 要判断的文件
	 * @return
	 */
	
	 public static boolean isImage(File file){
		 InputStream inputStream=null;
		try {
			inputStream = new FileInputStream(file);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		}
		
	        Image img;
	        try {
	            img = ImageIO.read(inputStream);
	            return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
	        } catch (Exception e) {
	            return false;
	        }finally{
	        	try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	    }
	 
	  public static boolean isMp4(File file){
		  String reg = "(mp4|flv|avi|rm|rmvb|wmv)";
	      Pattern p = Pattern.compile(reg);
	      boolean boo = p.matcher(file.getName()).find();
		  return boo;
	  }
	 
	 
	 /**
	  * 
	  * 
	  * @param srcPath 源完整路径
 	  * @param destPath 目标目录
	  * @return 正常返回文件名,返回null移动失败
	  */
	 public static String move(String srcPath,String destPath){
		 String name = srcPath.substring(srcPath.lastIndexOf("/"));
		 System.out.println("name:"+name);
		 destPath+=name;
		 File src = new File(srcPath);
		 File dest = new File(destPath);
		 System.out.println("before文件不存在？"+srcPath);
		 if(!src.exists())
			 return null;

         if(!dest.getParentFile().exists()&&!dest.getParentFile().mkdirs()){
        	 System.err.println(dest.getParent()+"创建失败...");
        	 return null;
         }
        	 
		 try {
			Files.move(src.toPath(), dest.toPath(),StandardCopyOption.ATOMIC_MOVE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		 
         System.out.println("返回true");
		 return name;
	 }
	 
	 
	 /**
	  * 
	  * 
	  * @param srcPath 源完整路径
 	  * @param destPath 目标目录
 	  * @param name 自定义文件名
	  * @return 返回null移动失败
	  */
	 public static String move(String srcPath,String destPath,String name){

		 destPath=destPath+"\\"+name;
		 File src = new File(srcPath);
		 File dest = new File(destPath);
		 System.out.println("before文件不存在？"+srcPath);
		 if(!src.exists())
			 return null;

         if(!dest.exists()&&dest.getParentFile().mkdirs())
        	 return null;
		 try {
			Files.move(src.toPath(), dest.toPath(),StandardCopyOption.ATOMIC_MOVE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		 
         System.out.println("返回true");
		 return name;
	 }
	 /**
	  * 返回:"文件名"
	  * @param srcPath
	  * @param destPath
	  * @return
	  */
	 
	 public static String copy(String srcPath,String destPath){
		 File src = new File(srcPath);
		 File dest = new File(destPath);
		 //判断文件夹是否存在，不存在就创建文件夹
		 if(!dest.exists()){
			 dest.getParentFile().mkdirs();
		 }
		 
		 Path path = null;
		 try {
			path  = Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name = path.toString();
		
		return name.substring(name.lastIndexOf("\\")+1);
	 }
	 
	
	
}
